/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System Driver Module                                  */
/*    Memory (ROM/RAM) access routines                                       */
/*                                                                           */
/*    Copyright (C) 2005 Laszlo Arvai                                        */
/*                                                                           */
/*****************************************************************************/

#ifndef __drvMemoryAccess_H
#define __drvMemoryAccess_H

///////////////////////////////////////////////////////////////////////////////
// Includes
#include "krnlTypes.H"

///////////////////////////////////////////////////////////////////////////////
// Windows emulation
///////////////////////////////////////////////////////////////////////////////
#ifdef WIN32

typedef int dosROMAddress;

void dosWinEmuROMInit(void);
dosByte dosWinEmuROMReadByte( int in_address );
dosWord dosWinEmuROMReadWord( int in_address );
dosDWord dosWinEmuROMReadDWord( int in_address );
dosWord dosWinEmuROMReadReverseWord( int in_address );

#define dosROMInit() dosWinEmuROMInit()
#define dosROMReadByte( x ) dosWinEmuROMReadByte(x)
#define dosROMReadWord( x ) dosWinEmuROMReadWord(x)
#define dosROMReadDWord( x ) dosWinEmuROMReadDWord(x)
#define dosROMReadReverseWord( x ) dosWinEmuROMReadReverseWord(x)

#else

/////////////////////////



///////////////////////////////////////////////////////////////////////////////
// Constants
#define	drvMEM_BankPort	PORTD
#define	drvMEM_BankDDR	DDRD
#define	drvMEM_BankBit	PD7

#define drvMEM_BANK0	0
#define drvMEM_BANK1	1

// Frame buffer memory address
#define drvFRAME_BUFFER_ADDRESS 0x1100

///////////////////////////////////////////////////////////////////////////////
// Types
typedef dosWord dosROMAddress;

///////////////////////////////////////////////////////////////////////////////
// ROM access macros
#define dosROMReadByte(addr)     					\
({                              					\
    uint16_t __addr16 = (uint16_t)(addr); \
    uint8_t __result;           					\
    __asm__                     					\
    (                           					\
        "elpm %0, Z+" "\n\t"     					\
        : "=r" (__result)       					\
        : "z" (__addr16)        					\
        : "r0"                  					\
    );                          					\
    __result;                   					\
})


#define dosROMReadWord( addr )  						\
({                                          \
    uint16_t __addr16 = (uint16_t)(addr);   \
    uint16_t __result;                      \
    __asm__                                 \
    (                                       \
        "elpm %A0, Z+"   "\n\t"             \
        "elpm %B0, Z"    "\n\t"             \
        : "=r" (__result), "=z" (__addr16)  \
        : "1" (__addr16)                    \
    );                                      \
    __result;                               \
})

#define dosROMReadDWord( addr ) 						\
({                                          \
    uint16_t __addr16 = (uint16_t)(addr);   \
    uint32_t __result;                      \
    __asm__                                 \
    (                                       \
        "elpm %A0, Z+"   "\n\t"             \
        "elpm %B0, Z+"   "\n\t"             \
        "elpm %C0, Z+"   "\n\t"             \
        "elpm %D0, Z"    "\n\t"             \
        : "=r" (__result), "=z" (__addr16)  \
        : "1" (__addr16)                    \
    );                                      \
    __result;                               \
})

///////////////////////////////////////////////////////////////////////////////
// Function prototypes
void krnlSelectRAMBank( dosByte in_bank);

#endif

#endif
